package com.mk.plugin.generator;

import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.fileTemplates.FileTemplateUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.mk.plugin.function.Functions;

import java.util.*;

public class ActionGenerator {

    public void generator(Project project, Properties p, PsiDirectory dir, List<Functions> functionsList) {
        PsiDirectory actionsDirectory = dir.createSubdirectory("actions");
        FileTemplate actionTemplate = FileTemplateManager.getInstance(project).getInternalTemplate("actions");
        FileTemplate actionTypeTemplate = FileTemplateManager.getInstance(project).getInternalTemplate("action_type");

        Map<String, Object> properties = new HashMap<>(); // 拷贝新的properties
        for (Object o : p.keySet()) {
            properties.put((String) o, p.get(o));
        }

        Set<String> actions = new HashSet<>();
        Set<String> actionTypes = new HashSet<>();

        for (Functions f : functionsList) {
            actions.add(f.getActionString(properties));
            actionTypes.add(f.getActionTypeString(properties));
        }

        properties.put("actionString", actions);
        properties.put("actionTypeString", actionTypes);

        try {
            FileTemplateUtil.createFromTemplate(actionTemplate, properties.get("underscore_name") + ".actions.tsx", properties, actionsDirectory, getClass().getClassLoader());
            FileTemplateUtil.createFromTemplate(actionTypeTemplate, properties.get("underscore_name") + ".action_type.tsx", properties, actionsDirectory, getClass().getClassLoader());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
